package com.wzgl.core.service;

import java.util.Date;

import javax.jws.WebParam;
import javax.jws.WebService;

import com.wzgl.core.base.JBaseController;
import com.wzgl.core.shop.ShopTseqCode;
import com.wzgl.core.utils.DateUtil;
import com.wzgl.core.utils.DateUtils;
import com.wzgl.core.utils.StringUtils;

import net.minidev.json.JSONValue;
import net.sf.json.JSONObject;
@WebService
public class ShoptSeqCodeService extends JBaseController {
	public String querySeqCode(@WebParam(name="params")String params){
		ParamsVo pv = new ParamsVo();
		pv.setCode(1);
		if(StringUtils.isNotBlank(params)){
			JSONObject json = JSONObject.fromObject(params);
			String type = json.getString("type");
			String sequence = json.getString("sequence");
			String dicinfo = json.getString("dicinfo");
			String fdeviceCode = json.getString("fdeviceCode");
			
			if(StringUtils.isNotBlank(type)){
				//序列码激活
				if(StringUtils.isBlank(sequence)){
					pv.setMsg("【sequence】序列码参数为空！");
					return JSONValue.toJSONString(pv);
				}
				//游戏字典
				if(StringUtils.isBlank(dicinfo)){
					pv.setMsg("【dicinfo】游戏字典参数为空！");
					return JSONValue.toJSONString(pv);
				}
				//设备编码
				if(StringUtils.isBlank(fdeviceCode)){
					pv.setMsg("【fdeviceCode】设备编码参数为空！");
					return JSONValue.toJSONString(pv);
				}
				ShopTseqCode sscode = ShopTseqCode.dao.findBySequence(sequence);
				if(sscode == null){
					pv.setMsg("未找到当前激活序列号！");
					return JSONValue.toJSONString(pv);
				}
				if(sscode.getFendTime() != null){
					int days = DateUtils.getDayDiff(sscode.getFendTime(), new Date());
					if(days < 0){
						pv.setMsg("当前激活码已经在"+sscode.getFendTime()+"过期!");
						return JSONValue.toJSONString(pv);
					}
				}
				if("1".equals(type)){
					if(sscode.getFactivTime() != null){
						if(sscode.getFdeviceCode().equals(fdeviceCode)){
							pv.setCode(0);
							return JSONValue.toJSONString(pv);
						}else{
							pv.setMsg("当前激活码已经在"+sscode.getFactivTime()+"激活!");
							return JSONValue.toJSONString(pv);
						}
					}
					sscode.setFactivTime(new Date());
					sscode.setFendTime(DateUtil.fomatDateDay(DateUtil.getAfterDayDate(sscode.getFcycle()+"")));
					sscode.setFdeviceCode(fdeviceCode);
					boolean b = sscode.update();
					if(!b){
						pv.setMsg("激活码失败,请联系管客服！");
						return JSONValue.toJSONString(pv);
					}
				}else{
					if(sscode.getFactivTime() == null){
						pv.setMsg("当前激活码未激活!");
						return JSONValue.toJSONString(pv);
					}
					if(!sscode.getFdeviceCode().equals(fdeviceCode)){
						pv.setMsg("激活设备编码不匹配！");
						return JSONValue.toJSONString(pv);
					}
				}
			}else{
				pv.setCode(1);
				pv.setMsg("【type】方法类型参数为空！");
				return JSONValue.toJSONString(pv);
			}
		}else{
			pv.setCode(1);
			pv.setMsg("参数为空！");
			return JSONValue.toJSONString(pv);
		}
		pv.setCode(0);
		return JSONValue.toJSONString(pv);
	}
}
